﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using skhukb.Models;
using skhukb.BL;
using skhukb.DB;

namespace skhukb.Controllers
{
    public class UserController : Controller
    {
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [skhukb.Util.AjaxOnly] [HttpPost]
        public ActionResult Login(string userid, string passwd, string returnUrl) // Referenced By: User/Login.cshtml
        {
            System.Threading.Thread.Sleep(1000);
            using (var db = new DBDataContext()) {
                var user = db.사용자.FirstOrDefault(p => p.로그인ID == userid);
                if (user != null && passwd == user.비밀번호) {
                    현재사용자.로그인(user);
                    return Json(new JsonModel { success = true, url = returnUrl });
                }

                string 에러메시지;
                if (현재사용자.Forest로그인(userid, passwd, out 에러메시지))
                    return Json(new JsonModel { success = true, url = returnUrl });
                else {
                    로그기록관리.로그인실패_기록(userid, passwd, returnUrl);
                    return Json(new JsonModel { success = false, message = 에러메시지 });
                }
            }
        }

        [Authorize]
        public ActionResult Logout()
        {
            현재사용자.로그아웃();
            return Redirect("~/");
        }
    }
}
